﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Media;
using HeritageAcademy.Models.DataModels;
using HeritageAcademy.Models.Loaders;

namespace HeritageAcademy.Models.ViewModels
{
    public class OptionsViewModel : ViewModelBase
    {
		#region Parameters 
        
        public static List<ColorSchemeViewModel> ColorSchemeOptions;
        public static List<LineSpacingViewModel> LineSpacingOptions;
        public static List<Double> ZoomOptions;

        private static OptionsViewModel _current;
        private static readonly SettingsConfigLoader _settingsConfigLoader;
        private static UsersConfigLoader _usersConfigLoader;
        private static Setting _setting;
		
        #endregion

		#region Ctor 
        
        static OptionsViewModel()
        {
            // LineSpacingOptions
            LineSpacingOptions = new List<LineSpacingViewModel>
                                     {
                                         new LineSpacingViewModel {Name = "Малое", LineSpacing = 1.25},
                                         new LineSpacingViewModel {Name = "Норма", LineSpacing = 1.5},
                                         new LineSpacingViewModel {Name = "Большое", LineSpacing = 1.7},
                                         new LineSpacingViewModel {Name = "Прибольшое", LineSpacing = 2.0}
                                     };

            // ZoomOptions
            ZoomOptions = new List<double> {136, 124, 112, 100, 88};

            // ColorSchemeOptions
            ColorSchemeOptions = new List<ColorSchemeViewModel>
                                     {
                                         new ColorSchemeViewModel
                                             {
                                                 Name = "Модерн",
                                                 ResourceUri =
                                                     new Uri(@"\Resources\ColorSchemes\Modern.xaml", UriKind.Relative),
                                                 PageColor = Colors.White,
                                                 TextColor = Color.FromArgb(0xFF, 0x40, 0x40, 0x40)
                                             },
                                         new ColorSchemeViewModel
                                             {
                                                 Name = "Бумага",
                                                 ResourceUri =
                                                     new Uri(@"\Resources\ColorSchemes\Paperback.xaml", UriKind.Relative),
                                                 PageColor = Color.FromArgb(0xFF, 0xEF, 0xEC, 0xE6),
                                                 TextColor = Color.FromArgb(0xFF, 0x4C, 0x41, 0x2E)
                                             },
                                         new ColorSchemeViewModel
                                             {
                                                 Name = "Контраст",
                                                 ResourceUri =
                                                     new Uri(@"\Resources\ColorSchemes\LightsOut.xaml", UriKind.Relative),
                                                 PageColor = Color.FromArgb(0xFF, 0x40, 0x40, 0x40),
                                                 TextColor = Color.FromArgb(0xFF, 0xB0, 0xB0, 0xB0)
                                             }
                                     };
            // Initialize all settings
            _settingsConfigLoader = new SettingsConfigLoader(new Settings());          
        }

        public void SetParameters()
        {
            // find active user id and its settings
            _usersConfigLoader = new UsersConfigLoader(new Users());
            _setting = (_usersConfigLoader.ActiveUser != null) ? _settingsConfigLoader.Model.GetSettingFromId(_usersConfigLoader.ActiveUser.Id)
                                                               : _settingsConfigLoader.Model.GetSettingFromId(_usersConfigLoader.Model.User[1].Id);
            Application.Current.Resources.MergedDictionaries[0].Source = ColorScheme.ResourceUri;
            RaisePropertyChanged("Zoom");
            RaisePropertyChanged("LineSpacing");
            RaisePropertyChanged("ColorScheme");
        }
        
        #endregion 

		#region Properties 
        
        public static OptionsViewModel Current
        {
            get { return _current ?? (_current = new OptionsViewModel()); }
        }

        public ColorSchemeViewModel ColorScheme
        {
            get { return (_setting != null) ? GetColorSchemeFromName(_setting.ColorScheme) : ColorSchemeOptions[0]; }
            set
            {
                if (value == null || value.Name == _setting.ColorScheme) return;
                Application.Current.Resources.MergedDictionaries[0].Source = GetColorSchemeFromName(value.Name).ResourceUri;
                _setting.ColorScheme = value.Name;
                _settingsConfigLoader.Save();
                RaisePropertyChanged("ColorScheme");
            }
        }

        public double Zoom
        {
            get { return (_setting != null) ? Convert.ToDouble(_setting.Zoom) : ZoomOptions[3]; }
            set
            {
                _setting.Zoom = value.ToString();
                _settingsConfigLoader.Save();
                RaisePropertyChanged("Zoom");
            }
        }

        public LineSpacingViewModel LineSpacing
        {
            get { return (_setting != null) ? GetLineSpacingFromValue(Convert.ToDouble(_setting.LineSpacing))
                                            : LineSpacingOptions[1]; }
            set
            {
                _setting.LineSpacing = value.LineSpacing.ToString();
                _settingsConfigLoader.Save();
                RaisePropertyChanged("LineSpacing");
            }
        }
        
        #endregion

		#region Public Methods 
        
        public static ColorSchemeViewModel GetColorSchemeFromName(string name)
        {
            var result = ColorSchemeOptions[0];
            foreach (var c in ColorSchemeOptions.Where(c => c != null && c.Name.Equals(name)))
                result = c;
            return result;
        }

        public static LineSpacingViewModel GetLineSpacingFromValue(double value)
        {
            var result = LineSpacingOptions[0];
            var delta = double.MaxValue;

            foreach (var l in LineSpacingOptions)
            {
                var d = Math.Abs(l.LineSpacing - value);
                if (d >= delta) continue;
                result = l;
                delta = d;
            }

            return result;
        }
		
        #endregion
    }

    public class ColorSchemeViewModel
    {
		#region Properties 
        
        public string Name { get; set; }
        public Color PageColor { get; set; }
        public Uri ResourceUri { get; set; }
        public Color TextColor { get; set; }
		
        #endregion Properties 
    }

    public class LineSpacingViewModel
    {
		#region Properties 
        
        public double LineSpacing { get; set; }
        public string Name { get; set; }
		
        #endregion Properties 
    }
}
